<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;

use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Art;
use GrahamCampbell\Markdown\Facades\Markdown;
class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = DB::table('test')->orderBy('id','desc')->paginate(2);
  
    
        return view('art.index',['data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('art.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //


         $val = Validator::make($_POST,[
            'title' => 'required|max:18|min:6',
            'content' => 'required|max:3000|min:50',
        ]);
           
        if ($val->fails()){
           
            return back()->withErrors($val)->withInput();
                     
        }





        $data = $request->except('_token');
        $res = DB::table('test')->insert($data);
        if($res){
            return redirect('/index');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
     
        $one = DB::table('test')->where('id','=',$id)->get();
        $one[0]->content=Markdown::convertToHtml($one[0]->content);
        return view('art.show',['one'=>$one]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $one = DB::table('test')->where('id','=',$id)->get();
        return view('art.edit',['one'=>$one]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {   
        $ser = DB::table('test')
            ->where('id','=', $id)
            ->update(['title' => $request['title'],'content'=>$request['content']]);
      
            return redirect('/index');
        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        $art = DB::table('test')->where('id','=',$id)->delete();
        if($art){
            return redirect('/index');
        }else{
            return back();


       
    }
    }
}
